//
//  ParkDetailView.m
//  bbqpitbooking
//
//  Created by Yin Ni Tun on 6/3/11.
//  Copyright 2011 iss. All rights reserved.
//

#import "ParkDetailView.h"

@implementation ParkDetailView

@synthesize postalCode;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	return [self init];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSLog(@"parkviewdid load");
    [super viewDidLoad];
	//[self parkDetailReturn];

}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

-(id) init
{
	return [super initWithNibName:@"ParkDetailView" bundle:nil];
}
-(BOOL)parkDetailReturn
{
	
	NSString *str= self.postalCode;
	self.title = str;
	NSString *urlLocation = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
							 [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	
	
	
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlLocation] encoding:NSASCIIStringEncoding
														   error:nil];
	
    NSArray *itemsLocation = [locationString componentsSeparatedByString:@","];
	
	
    double latitude = 0.0;
    double longitude = 0.0;
    if([itemsLocation count] >= 4 && [[itemsLocation objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[itemsLocation objectAtIndex:2] doubleValue];
        longitude = [[itemsLocation objectAtIndex:3] doubleValue];
    }
    else {
        NSLog(@"Cannot locate the location");
    }
	
	
    CLLocationCoordinate2D newCoordinate;
    newCoordinate.latitude = latitude;
    newCoordinate.longitude = longitude;
	
	MapAnnotation *ma = [[MapAnnotation alloc] initWithCoordinate:newCoordinate title:str];
	
	
	[parkMapView addAnnotation:ma];
	[parkMapView setCenterCoordinate:newCoordinate animated:YES];
	
	// structure to define areas spanned by a map region
	MKCoordinateRegion region; 
	
    region.center = newCoordinate;
 	// define the distance (in degrees) to be display
	region.span.latitudeDelta = .005;
	region.span.longitudeDelta = .005;
	
    [parkMapView setRegion:region animated:TRUE];
	
	[ma release];
	//[tf resignFirstResponder];
	return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
	
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[parkMapView release];
}


@end
